package com.demo.maintenance.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.demo.maintenance.R;
import com.demo.maintenance.bean.BaseStringResponse;
import com.demo.maintenance.bean.UserResponse;
import com.demo.maintenance.contant.Config;
import com.demo.maintenance.contant.Global;
import com.demo.maintenance.utils.AntiShakeUtils;
import com.demo.maintenance.utils.HttpManager;
import com.demo.maintenance.utils.SystemBarTintManager;
import com.hjq.toast.ToastUtils;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

/**
 * 登录页
 */
public class LoginActivity extends AppCompatActivity {

    private EditText et_username;

    private EditText et_password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        QMUIStatusBarHelper.translucent(this);
        SystemBarTintManager.setStatusBarDarkTheme(this, true);
        setContentView(R.layout.activity_login);

        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);

        // 登录按钮
        findViewById(R.id.btn_login).setOnClickListener(v -> {
            if (AntiShakeUtils.isInvalidClick(v)){
                return;
            }
            if (TextUtils.isEmpty(et_username.getText().toString())){
                ToastUtils.show("Please Input Account");
                return;
            }
            if (TextUtils.isEmpty( et_password.getText().toString())){
                ToastUtils.show("Please Input Password");
                return;
            }
           login();
        });

    }

    private void login(){
        WaitDialog.show("Loading");

        String url = Global.ipAddress + "/app/loginInner" +
                "?name=" + et_username.getText().toString()
                + "&password=" + et_password.getText().toString();

        HttpManager.get(url, new HttpManager.NetworkCallBack() {
            @Override
            public void onSuccess(String successInfo) {
                UserResponse userResponse = JSON.parseObject(successInfo, UserResponse.class);
                if (userResponse.code != 200){
                    ToastUtils.show(userResponse.msg);
                    return;
                }
                try {
                    Global.userResponse = userResponse;

                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    LoginActivity.this.finish();
                }catch (Exception e){
                    e.printStackTrace();
                    ToastUtils.show("Login Failed !");
                }
            }

            @Override
            public void onError(String errorInfo) {
                Log.e("monitoring", errorInfo);
                ToastUtils.show("Network Error !");
            }

            @Override
            public void onFinal() {
                WaitDialog.dismiss();
            }
        });
    }

    @Override
    public boolean moveTaskToBack(boolean nonRoot) {
        return super.moveTaskToBack(true);
    }
}
